package com.hyperfix.washer.controller;

import com.hyperfix.models.common.R;
import com.hyperfix.models.washer.dtos.*;
import com.hyperfix.washer.service.RecordService;
import com.hyperfix.washer.service.RiskRecordService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author JiangChunYu
 * 2024/5/3
 */
@Validated
@RestController
@RequestMapping("/statistic")
public class StatisticController {
    @Resource
    private RecordService recordService;
    @Resource
    private RiskRecordService riskRecordService;

    /**
     * 统计指定井队今天的日累计流量和本周的日累计流量总和
     *
     * @param teamId 井队 ID
     * @return {@link R}<{@link TotalFlowStatistic}> 统计结果
     */
    @PostMapping("/team/{teamId}/totalFlow")
    public R<TotalFlowStatistic> statisticTeamTotalFlow(@PathVariable @NotNull Integer teamId) {
        Long dayTotalFlow = recordService.statisticDayTotalFlow(teamId);
        Long weekTotalFlow = recordService.statisticWeekTotalFlow(teamId);
        TotalFlowStatistic riskRecordStatistic = TotalFlowStatistic.builder()
                .dayTotalFlow(dayTotalFlow)
                .weekTotalFlow(weekTotalFlow)
                .build();
        return R.success(riskRecordStatistic);
    }

    /**
     * 统计指定井队异常数据中各参数类型的数量
     *
     * @param teamId 井队 ID
     * @return {@link R}<{@link Map}<{@link String},{@link Integer}>> 统计结果
     */
    @PostMapping("/team/{teamId}/paramType")
    public R<Map<String, Integer>> statisticTeamParamType(@PathVariable @NotNull Integer teamId) {
        List<ParamTypeStatistic> paramTypeStatistics = recordService.statisticWeekParamType(teamId);
        Map<String, Integer> paramTypeStatisticsMap = paramTypeStatistics.stream()
                .collect(Collectors.toMap(ParamTypeStatistic::getName, ParamTypeStatistic::getValue));
        return R.success(paramTypeStatisticsMap);
    }

    /**
     * 统计指定井队下所有站场本周的异常数据量
     *
     * @param teamId 团队 ID
     * @return {@link R}<{@link List}<{@link StationRiskRecord}>> 统计结果
     */
    @PostMapping("/team/{teamId}/riskRecord")
    public R<List<StationRiskRecord>> statisticTeamRiskRecord(@PathVariable @NotNull Integer teamId) {
        List<StationRiskRecord> stationRiskRecordList = recordService.statisticTeamRiskRecord(teamId);
        return R.success(stationRiskRecordList);
    }

    /**
     * 统计指定井队下所有管线本周每天的日累计流量 并按站场分类
     *
     * @param teamId 井队 ID
     * @return {@link R}<{@link TeamTotalFlow}> 统计结果
     */
    @PostMapping("/team/{teamId}/weekTotalFlow")
    public R<TeamTotalFlow> teamWeekTotalFlow(@PathVariable @NotNull Integer teamId) {
        TeamTotalFlow teamTotalFlow = recordService.teamWeekTotalFlow(teamId);
        return R.success(teamTotalFlow);
    }

    /**
     * 统计指定井队下所有站场今日00：00-23：00点的日累计流量总和走趋
     *
     * @param teamId 井队 ID
     * @return {@link R}<{@link TeamTotalFlowTrend}> 统计结果
     */
    @PostMapping("/team/{teamId}/dayTotalFlowTrend")
    public R<TeamTotalFlowTrend> dayTotalFlowTrend(@PathVariable @NotNull Integer teamId) {
        TeamTotalFlowTrend teamTotalFlowTrend = recordService.dayTotalFlowTrend(teamId);
        return R.success(teamTotalFlowTrend);
    }

    /**
     * 统计指定井队的风险记录
     * @return {@link R}<{@link RiskRecordStatistic}> 统计结果
     */
    @PostMapping("/team/{teamId}/teamRiskRecord")
    public R<RiskRecordStatistic> teamRiskRecord(@PathVariable @NotNull Integer teamId) {
        Integer dayRiskRecordNum = riskRecordService.statisticDay(teamId);
        Integer weekRiskRecordNum = riskRecordService.statisticWeek(teamId);
        RiskRecordStatistic riskRecordStatistic = RiskRecordStatistic.builder()
                .dayRiskRecordNum(dayRiskRecordNum)
                .weekRiskRecordNum(weekRiskRecordNum)
                .build();
        return R.success(riskRecordStatistic);
    }
}
